package com.cwx.web;

import com.alibaba.fastjson.JSONObject;
import com.cwx.common.AjaxResult;
import com.cwx.dao.entity.OrderInfo;
import com.cwx.interfaces.rong360.Rong360Interface;
import com.cwx.service.OrderInfoService;
import com.cwx.utils.Tool;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by code on 17-8-31.
 */
@Log4j
@RestController
@RequestMapping("/rong360")
public class Rong360Controller {

    @Autowired
    private OrderInfoService orderInfoService;

    @RequestMapping("/callback")
    public String callback(HttpServletRequest request){
        String outUniqueId = request.getParameter("outUniqueId");
        String userId = request.getParameter("userId");
        String state = request.getParameter("state");
//        String account = request.getParameter("account");
        String accountType = request.getParameter("accountType");
        String search_id = request.getParameter("search_id");
        log.info("ID为" + userId +"的用户调用" + accountType + "状态为" + state);
        if (state.equals("report")){
            Map<String,Object> params = new HashMap<>();
            try {
                JSONObject result = null;
                log.info("-----------------------ID为" + userId + "的用户调用" + accountType + "的数据-----------------------------");
                OrderInfo orderInfo = orderInfoService.findById(Long.parseLong(userId));
                switch (accountType){
                    case "jd":
                        params.put("userId",userId);
                        params.put("outUniqueId",outUniqueId);
                        params.put("reportType","html");
                        result = Rong360Interface.getReportDetail(params);
                        orderInfo.setJdData(result.toJSONString());
                        break;
                    case "mobile":
                        params.put("userId",userId);
                        params.put("outUniqueId",outUniqueId);
                        params.put("reportType","html");
                        result = Rong360Interface.getReportDetail(params);
                        orderInfo.setMobileData(result.toJSONString());
                        break;
                    case "chsi":
                        params.put("user_id",search_id);
                        params.put("merchant_id",Rong360Interface.app_id);
                        result = Rong360Interface.getChsi(params);
                        orderInfo.setChsiData(result.toJSONString());
                        break;
                    default:
                        break;
                }
                orderInfoService.save(orderInfo);
                log.info("-----------------------ID为" + userId + "的用户调用" + accountType + "的数据-----------------------------");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("ID为" + userId + "的用户调用" + accountType + "获取报告时出现异常:\n" + e.getMessage());
            }
        }
        return "success";
    }

    @RequestMapping("/collectUser/{type}")
    public AjaxResult insure(@PathVariable String type,Long orderId){
        AjaxResult ajaxResult = new AjaxResult();
        Map<String,Object> params = new HashMap<>();
        OrderInfo orderInfo = orderInfoService.findById(orderId);
        params.put("type",type);
        params.put("platform","web");
        params.put("name", orderInfo.getRealName());
        params.put("phone",orderInfo.getPhone());
        params.put("idNumber",orderInfo.getIdCard());
        params.put("userId",orderId);
        params.put("outUniqueId",System.currentTimeMillis() + Tool.randomStrNum(100000,999999));
        params.put("notifyUrl", Rong360Interface.CALLBACK_URL);
        if (type.equals("mobile")){
            params.put("returnUrl",Rong360Interface.RETURN_URL_TOTB);
        }else {
            params.put("returnUrl",Rong360Interface.RETURN_URL_TOJD);
        }
        try {
            JSONObject result = Rong360Interface.collectUser(params);
            int error = result.getInteger("error");
            if (error == 200){
                log.info("融360" + type + "接口调用成功" + orderInfo.getPhone());
                System.out.println(result);
                ajaxResult.setSuccess(true).setMessage(type + "接口调用成功").setData(result);
            }else {
                log.error("融360" + type + "接口调用失败" + orderInfo.getPhone() + ":\n" + error);
                ajaxResult.setSuccess(false).setMessage(type + "接口调用失败").setData(result);
            }
        } catch (Exception e) {
            log.error("调用融360" + type + "接口出现异常" + orderInfo.getPhone() + ":\n" + e.getMessage());
            ajaxResult.setSuccess(false).setMessage(type + "接口调用异常");
        }
        return ajaxResult;
    }
}
